package com.jianganming.singleton;

/**
 * 线程内部全局单例
 */
public class ThreadLocalSingleton {
    private ThreadLocalSingleton(){

    }
    private static final ThreadLocal<ThreadLocalSingleton> THREAD_LOCAL
            = ThreadLocal.withInitial(()->new ThreadLocalSingleton());
    public static ThreadLocalSingleton getInstance(){
        return THREAD_LOCAL.get();
    }

    public static void main(String[] args) {
        new Thread(()->{
            System.out.println(ThreadLocalSingleton.getInstance());
            System.out.println(ThreadLocalSingleton.getInstance());
        }).start();
        new Thread(()->{
            System.out.println(ThreadLocalSingleton.getInstance());
            System.out.println(ThreadLocalSingleton.getInstance());
        }).start();
    }
}
